//
//  XNXNStatusPictureView.swift
//  weibo
//
//  Created by 王向宁 on 16/5/26.
//  Copyright © 2016年 王向宁. All rights reserved.
//显示微博的配图视图

import UIKit

private let pictureMargin: CGFloat = 3

private let maxWidth = ScreenWidth - 2 * StatusCellMargin

private let itemWidth = (maxWidth - 2 * pictureMargin) / 3

private let pictureCellId = "pictureCellId"


class XNXNStatusPictureView: UICollectionView {
    
    var pictures: [XNPicture]? {
        didSet {
           // print(pictures?.count)
            testLabel.text = "\(pictures?.count ?? 0)"
            
            let pSize = calculatePictureViewSize()
            
            self.snp_updateConstraints { (make) in
                make.size.equalTo(pSize)
            }
            
            reloadData()
            
        }
    }

    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        let layout = UICollectionViewFlowLayout()
        
        //设置间距
        layout.minimumLineSpacing = pictureMargin
        
        layout.minimumInteritemSpacing = pictureMargin
        
        super.init(frame: frame, collectionViewLayout: layout)
        
        self.dataSource = self
        
        //注册
        registerClass(XNStatusPictureCell.self, forCellWithReuseIdentifier: pictureCellId)
        
        setupUI()
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
        addSubview(testLabel)
        
        testLabel.snp_updateConstraints { (make) in
            make.center.equalTo(self.snp_center)
        }
    }
    
    private lazy var testLabel: UILabel = UILabel(title: "", font: 30, textColor: UIColor.redColor())
    
    
    func calculatePictureViewSize() -> CGSize {
        let imageCount = pictures?.count ?? 0
        
        if imageCount == 0 {
            return CGSizeZero
        }
        
        let layout = self.collectionViewLayout as! UICollectionViewFlowLayout
        
        
        if imageCount == 1 {
            let imageSize = CGSizeMake(100, 150)
            
            layout.itemSize = imageSize
            
            return imageSize
            
        }
        
        layout.itemSize = CGSizeMake(itemWidth, itemWidth)
        
        if imageCount == 4 {
            let w = itemWidth * 2 + pictureMargin
            
            return CGSizeMake(w, w)
            
        }
        
        let row = CGFloat((imageCount - 1) / 3) + 1
        
        let maxHeight = row * itemWidth + (row - 1) * pictureMargin
        
        return CGSizeMake(maxWidth, maxHeight)
   
    }
}

extension XNXNStatusPictureView : UICollectionViewDataSource {
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return pictures?.count ?? 0
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(pictureCellId, forIndexPath: indexPath) as! XNStatusPictureCell
        
        cell.picture = pictures![indexPath.item]
        
        cell.backgroundColor = randomColor()
        
        return cell
    }
}

class XNStatusPictureCell: UICollectionViewCell {
    var picture: XNPicture? {
        didSet {
            let url = NSURL(string: picture?.thumbnail_pic ?? "")
            
            imageView.sd_setImageWithURL(url)
            
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
        contentView.addSubview(imageView)
        
        imageView.snp_updateConstraints { (make) in
            make.edges.equalTo(contentView.snp_edges)
        }
    }
    
    private lazy var imageView: UIImageView = {
       let iv = UIImageView()
        
        iv.contentMode = .ScaleAspectFill
        
        //手写代码需要自动控制裁剪,xib/sb会自动设置裁剪
        iv.clipsToBounds = true
        
        return iv
    }()
}

